/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.veterinarySystem.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.veterinarySystem.bl.DuenoMascotaBlLocal;
import mx.its.dsw2.veterinarySystem.modelo.DuenoMascota;

/**
 *
 * @author 100101
 */
@ManagedBean
@SessionScoped
public class DuenoMascotaBean {

 @EJB
    private DuenoMascotaBlLocal duenoMascotaBl;
    private List<DuenoMascota> lista;
    private DuenoMascota duenoMascota;
    private String accion="";
    /**
     * Creates a new instance of DuenoMascotaBean
     */
    public DuenoMascotaBean() {
        duenoMascota=new DuenoMascota();
    }

    public DuenoMascota getDuenoMascota() {
        return duenoMascota;
    }

    public void setDuenoMascota(DuenoMascota duenoMascota) {
        this.duenoMascota = duenoMascota;
    }

    public List<DuenoMascota> getLista() {
        lista=duenoMascotaBl.getAll();
        return lista;
    }
    
    public String procesarAccion(){
        if(accion.equalsIgnoreCase("Nuevo")){
        duenoMascotaBl.register(duenoMascota);
        }else{
            if(accion.equalsIgnoreCase("Editar")){
                duenoMascotaBl.modify(duenoMascota);
            }else{
                if(accion.equalsIgnoreCase("Eliminar")){
                    duenoMascotaBl.romove(duenoMascota);
                }
            }
        }
        accion="";
        return "DuenoMascotaList";
    }
    
    public void prepararNuevo(ActionEvent evt){
        accion="Nuevo";
        duenoMascota=new DuenoMascota();
    }
    
    public void prepararEditar(ActionEvent evt){
        accion="Editar";
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        duenoMascota=lista.get(index);
    }
    
    public void prepararEliminar(ActionEvent evt){
        accion="Eliminar";
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        duenoMascota=lista.get(index);
    }
    
    public String llamadaPaginas(){
        String link="";
        if(accion.equalsIgnoreCase("Editar")||accion.equalsIgnoreCase("Nuevo")){
            link="DuenoMascotaCreateEdit";
        }else{
            if(accion.equalsIgnoreCase("Eliminar")){
                link="DuenoMascotaList";
            }
        }
        return link;
    }
}
